package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.mq.Constants;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Override
    @Transactional(rollbackFor = Exception.class) //默认只能对RuntimeException进行回滚
    public void updateStatus(Long id, Integer status) {
        //update tb_item set status = ? where id = ?
        update(Wrappers.<Item>lambdaUpdate()
                .eq(Item::getId,id)
                .set(Item::getStatus,status));

        rabbitTemplate.convertAndSend(Constants.UP_OR_DOWN_TOPIC,
                status == 1 ? Constants.UP_ROUTING_KEY : Constants.DOWN_ROUTING_KEY,id);
    }
}
